/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interface.Panel;

import Controller.InputController;
import Controller.VisitController;
import DAOs.CollaboratorJpaController;
import DAOs.exceptions.NonexistentEntityException;
import Entity.Collaborator;
import Entity.ReforestationZone;
import Entity.Visit;
import java.util.List;
import javax.persistence.EntityManagerFactory;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author Nicolas
 */
public class PanelVisitCreate extends javax.swing.JPanel {

    /**
     * Creates new form PanelVisitCreate
     */
    private int VISIT_DURATION = 3;
    private Visit visit;
    private VisitController vc;
    private JFrame frame = new JFrame();
    private EntityManagerFactory emf;
    private ReforestationZone zone;
    JFrame parent;
    
    public PanelVisitCreate(EntityManagerFactory emf, ReforestationZone zone, JFrame parent) {
        initComponents();
        this.emf = emf;
        this.zone = zone;
        this.parent = parent;
        vc = new VisitController(this.emf);
        inputController = new InputController();
        horaInicioSpinField.setMinimum(0);
        horaInicioSpinField.setValue(8);
        horaFinSpinField.setMinimum(horaInicioSpinField.getValue());
        horaFinSpinField.setValue(20);
        horaInicioSpinField.setMaximum(horaFinSpinField.getValue());
        horaFinSpinField.setMaximum(23);
        colaboradorVisitaTextArea.setEditable(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel3 = new javax.swing.JPanel();
        logoLabel2 = new javax.swing.JLabel();
        titleLabel2 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jPanel1 = new javax.swing.JPanel();
        colaboradorNITBuscarVisitaBtn = new javax.swing.JButton();
        colaboradorNITVisitaBuscarTF = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        colaboradorVisitaTextArea = new javax.swing.JTextArea();
        jLabel4 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        horaInicioSpinField = new com.toedter.components.JSpinField();
        fechaDeVisita = new com.toedter.calendar.JDateChooser();
        backBTN = new javax.swing.JButton();
        crearCitaBtn = new javax.swing.JButton();
        horaFinSpinField = new com.toedter.components.JSpinField();

        logoLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Interface/Resources/Visit big.png"))); // NOI18N

        titleLabel2.setFont(new java.awt.Font("Tahoma", 1, 36)); // NOI18N
        titleLabel2.setText("Registro de Nueva Visita ULATA");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(logoLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 128, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(titleLabel2)
                .addContainerGap(19, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addComponent(titleLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(logoLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Datos de Visita"));

        colaboradorNITBuscarVisitaBtn.setText("Buscar");
        colaboradorNITBuscarVisitaBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                colaboradorNITBuscarVisitaBtnActionPerformed(evt);
            }
        });

        colaboradorNITVisitaBuscarTF.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                colaboradorNITVisitaBuscarTFActionPerformed(evt);
            }
        });

        colaboradorVisitaTextArea.setColumns(20);
        colaboradorVisitaTextArea.setRows(5);
        jScrollPane1.setViewportView(colaboradorVisitaTextArea);

        jLabel4.setText("Colaborador NIT:");

        jLabel1.setText("Fecha de Visita: ");

        jLabel2.setText("Hora de Inicio: ");

        jLabel3.setText("Hora de Fin:");

        horaInicioSpinField.setMaximum(23);
        horaInicioSpinField.setMinimum(0);
        horaInicioSpinField.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                horaInicioSpinFieldPropertyChange(evt);
            }
        });

        backBTN.setText("Volver");
        backBTN.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backBTNActionPerformed(evt);
            }
        });

        crearCitaBtn.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Interface/Resources/visit.png"))); // NOI18N
        crearCitaBtn.setText("Crear Visita");
        crearCitaBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                crearCitaBtnActionPerformed(evt);
            }
        });

        horaFinSpinField.setMaximum(23);
        horaFinSpinField.setMinimum(0);
        horaFinSpinField.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                horaFinSpinFieldPropertyChange(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(46, 46, 46)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(horaFinSpinField, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(horaInicioSpinField, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(fechaDeVisita, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(crearCitaBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(backBTN)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 41, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(colaboradorNITVisitaBuscarTF, javax.swing.GroupLayout.PREFERRED_SIZE, 154, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(colaboradorNITBuscarVisitaBtn)))
                .addGap(31, 31, 31))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(colaboradorNITBuscarVisitaBtn)
                            .addComponent(colaboradorNITVisitaBuscarTF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(73, 73, 73))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(fechaDeVisita, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(horaInicioSpinField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(horaFinSpinField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(crearCitaBtn)
                            .addComponent(backBTN))
                        .addContainerGap())))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(40, 40, 40)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 216, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(33, 33, 33))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void crearCitaBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_crearCitaBtnActionPerformed
        if(vc.createVisitPossible(fechaDeVisita.getCalendar(), horaInicioSpinField.getValue(), horaFinSpinField.getValue(), colaboradorNITVisitaBuscarTF.getText())){
            CollaboratorJpaController cJPA = new CollaboratorJpaController(emf);
            List<Collaborator> cList = cJPA.findCollaboratorEntities();
            Collaborator c = null;
            for(Collaborator u : cList){
                if(u.getCedula() == Integer.parseInt(colaboradorNITVisitaBuscarTF.getText())){
                    c = u;
                }
            }         
            visit = new Visit();
            visit.setId(this.zone.getId());
            visit.setFecha(fechaDeVisita.getCalendar());
            visit.setHoraInicio(horaInicioSpinField.getValue());
            visit.setHoraFin(horaFinSpinField.getValue());
            c.getVisits().add(visit);
            try {
                cJPA.edit(c);
                JOptionPane.showMessageDialog(frame, "Se ha creado la cita", "Succes", JOptionPane.INFORMATION_MESSAGE);
                parent.dispose();
            } catch (NonexistentEntityException ex) {
                JOptionPane.showMessageDialog(frame, "No se existe el Colaborador", "Error", JOptionPane.ERROR_MESSAGE);
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(frame, "Error", "Error", JOptionPane.ERROR_MESSAGE);
            }
        }else{
            JOptionPane.showMessageDialog(frame, "No se puede crear la cita", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_crearCitaBtnActionPerformed

    private void colaboradorNITVisitaBuscarTFActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_colaboradorNITVisitaBuscarTFActionPerformed
        colaboradorNITBuscarVisitaBtnActionPerformed(evt);
    }//GEN-LAST:event_colaboradorNITVisitaBuscarTFActionPerformed

    private void colaboradorNITBuscarVisitaBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_colaboradorNITBuscarVisitaBtnActionPerformed
        if (inputController.isInteger(colaboradorNITVisitaBuscarTF.getText())) {
            colaboradorVisitaTextArea.setText(vc.findCollaborator(colaboradorNITVisitaBuscarTF.getText()));
        }
    }//GEN-LAST:event_colaboradorNITBuscarVisitaBtnActionPerformed

    private void horaInicioSpinFieldPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_horaInicioSpinFieldPropertyChange
        horaFinSpinField.setMinimum(horaInicioSpinField.getValue() + VISIT_DURATION);
    }//GEN-LAST:event_horaInicioSpinFieldPropertyChange

    private void horaFinSpinFieldPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_horaFinSpinFieldPropertyChange
        horaInicioSpinField.setMaximum(horaFinSpinField.getValue() - VISIT_DURATION);
    }//GEN-LAST:event_horaFinSpinFieldPropertyChange

    private void backBTNActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backBTNActionPerformed
        parent.dispose();
    }//GEN-LAST:event_backBTNActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton backBTN;
    private javax.swing.JButton colaboradorNITBuscarVisitaBtn;
    private javax.swing.JTextField colaboradorNITVisitaBuscarTF;
    private javax.swing.JTextArea colaboradorVisitaTextArea;
    private javax.swing.JButton crearCitaBtn;
    private com.toedter.calendar.JDateChooser fechaDeVisita;
    private com.toedter.components.JSpinField horaFinSpinField;
    private com.toedter.components.JSpinField horaInicioSpinField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel logoLabel2;
    private javax.swing.JLabel titleLabel2;
    // End of variables declaration//GEN-END:variables
    private InputController inputController;
}
